// src/utils/wecomConfig.ts
import {configSignature, getCorpAndAgentInfo} from "@/api/work";
import * as ww from '@wecom/jssdk';
import {OpenUserProfileType} from "@wecom/jssdk";
// 获取 corpId 和 agentId 的方法
const getCorpAndAgentId = async () => {
    try {
        const response = await getCorpAndAgentInfo();
        return response.data;
    } catch (error) {
        console.error("Failed to get corpId and agentId:", error);
        throw new Error("Failed to get corpId and agentId");
    }
};

// 获取签名配置的接口
const getConfigSignature = async (url: string) => {
    try {
        const response = await configSignature(url);
        return response.data;
    } catch (error) {
        console.error("Failed to get config signature:", error);
        throw new Error("Failed to get config signature");
    }
};

export const initWork = async () => {
    try {
        const { corpId, agentId } = await getCorpAndAgentId();
        ww.register({
            corpId,
            agentId,
            jsApiList: ['openUserProfile'],
            getConfigSignature
        });
    }catch (e) {
        console.error("Failed to get config signature:", e);
    }
};


export const openUserProfile = (type:OpenUserProfileType,userId:string) => {
    ww.openUserProfile({
        type: type,
        userid: userId
    })
};